﻿
using System;
using System.Collections.Generic;
using System.Data;
using QLKS.Entity;
using System.Linq;

namespace QLKS.DAL
{
    public class DMKhachHangDAO
    {
        private SqlDataProvider dataProvider;

        public DMKhachHangDAO()
        {
            dataProvider = new SqlDataProvider();
        }

        public List<DMKhachHang> GetByAll(DMKhachHang obj)
        {
            List<DMKhachHang> lstData = new List<DMKhachHang>(); ;
            try
            {
                DataTable dt = dataProvider.ExecuteDataTable("sp_DMKhachHang_GetByAll"
                    , new string[] { "MaKhachHang", "TenKhachHang", "LoaiKhachHang", "CMND", "DiaChi", "DienThoai", "Email", "GhiChu" }
                    , new object[] { obj.MaKhachHang, obj.TenKhachHang, obj.LoaiKhachHangID, obj.CMND, obj.DiaChi, obj.DienThoai, obj.Email, obj.GhiChu }
                );
                DMKhachHang entity;
                foreach(DataRow myrow in dt.Rows)
                {
                    entity = new DMKhachHang();
                    entity.MaKhachHang = Convert.ToInt32(myrow["MaKhachHang"]);
                    entity.TenKhachHang = myrow["TenKhachHang"] is DBNull ? string.Empty : myrow["TenKhachHang"].ToString();
                    entity.LoaiKhachHangID = Convert.ToInt32(myrow["LoaiKhachHang"]);
                    entity.CMND = myrow["CMND"] is DBNull ? string.Empty : myrow["CMND"].ToString();
                    entity.DiaChi = myrow["DiaChi"] is DBNull ? string.Empty : myrow["DiaChi"].ToString();
                    entity.DienThoai = myrow["DienThoai"] is DBNull ? string.Empty : myrow["DienThoai"].ToString();
                    entity.Email = myrow["Email"] is DBNull ? string.Empty : myrow["Email"].ToString();
                    entity.GhiChu = myrow["GhiChu"] is DBNull ? string.Empty : myrow["GhiChu"].ToString();
                    try
                    {
                        if (entity.LoaiKhachHangID != null && entity.LoaiKhachHangID != 0)
                        {
                            entity.LoaiKhachHang = ((new DMLoaiKhachHangDAO()).GetByAll(new DMLoaiKhachHang(entity.LoaiKhachHangID))).FirstOrDefault();
                        }
                        else
                        {
                            entity.LoaiKhachHang = null;
                        }
                    }
                    catch
                    {
                        entity.LoaiKhachHang = null;
                    }
                    lstData.Add(entity);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstData;
        }

        public bool Insert(DMKhachHang obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMKhachHang_Insert"
                   , new string[] { "MaKhachHang", "TenKhachHang", "LoaiKhachHang", "CMND", "DiaChi", "DienThoai", "Email", "GhiChu" }
                   , new object[] { obj.MaKhachHang, obj.TenKhachHang, obj.LoaiKhachHangID, obj.CMND, obj.DiaChi, obj.DienThoai, obj.Email, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }

        public bool Update(DMKhachHang obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMKhachHang_Update"
                   , new string[] { "MaKhachHang", "TenKhachHang", "LoaiKhachHang", "CMND", "DiaChi", "DienThoai", "Email", "GhiChu" }
                   , new object[] { obj.MaKhachHang, obj.TenKhachHang, obj.LoaiKhachHangID, obj.CMND, obj.DiaChi, obj.DienThoai, obj.Email, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }

        public bool Delete(DMKhachHang obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMKhachHang_Delete"
                   , new string[] { "MaKhachHang", "TenKhachHang", "LoaiKhachHang", "CMND", "DiaChi", "DienThoai", "Email", "GhiChu" }
                   , new object[] { obj.MaKhachHang, obj.TenKhachHang, obj.LoaiKhachHangID, obj.CMND, obj.DiaChi, obj.DienThoai, obj.Email, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }
    }
}
